package models

import (
	"registration-booking/global"
)

type TestBooking struct {
	ID
	CardId      uint `json:"card_id" gorm:"index:comment:就诊卡ID"`
	TestComboId uint `json:"test_combo_id" gorm:"index:comment:体检套餐ID"`
	TestDateId  uint `json:"test_date_id" gorm:"index:comment:体检日期ID"`
	UserId      uint `json:"user_id" gorm:"index:comment:用户ID"`
	Status      int  `json:"status" gorm:"index:comment:状态（0未就诊，1已就诊）" default:"0" `
	Timestamps
	SoftDeletes
}

func (testBooking TestBooking) Create() (err error) {
	err = global.App.DB.Create(&testBooking).Error
	textDate, err := FindTextDateById(testBooking.TestDateId)
	if err != nil {
		return err
	}
	textDate.Num--
	err = UpdateTestDate(textDate)
	return
}
func FindComboBookingHistory(uid uint) (testBookings []TestBooking, err error) {
	err = global.App.DB.Where("user_id = ? ", uid).Find(&testBookings).Error
	return
}

func FindComboBookingHistoryAll(pageNo, pageSize int) (testBookings []TestBooking, count int64, err error) {
	err = global.App.DB.Model(TestBooking{}).Count(&count).Offset(pageNo - 1*pageSize).Limit(pageSize).Find(&testBookings).Error
	return
}
func FindComboBookingHistoryById(bid, uid uint) (testBooking TestBooking, err error) {
	err = global.App.DB.Where("id = ? AND user_id = ? ", bid, uid).First(&testBooking).Error
	return
}
